/* file: https://fm-preview.cnbc.com/applications/cnbc.com/resources/files/2018/08/10/updateSavedSymbolsExpiry.js?vn=1533930678*/
window.postLoadFunctions = window.postLoadFunctions || {};
window.postLoadFunctions['updateSavedSymbolsExpiry'] = function() {

var theDate = new Date();
var oneYearLater = new Date( theDate.getTime() + 31536000000 );
var expiryDate = oneYearLater.toGMTString();

function savedSymbolsUpdate(symbols, limit) {

		var limit = limit || 25;
		var	savedViewedSymbols = (CNBC_Utils.readSavedSymbols().length > 0) ? CNBC_Utils.readSavedSymbols() : [];
		var mergedArray = removeDuplicates(savedViewedSymbols);
		if(mergedArray.length > limit) {
			mergedArray.length = limit;
		};

		function removeDuplicates(array) {
			console.log("remove dupes is called")
			var tempObj= {};
			var tempArray = [];
			for(var i=0; i < array.length; i++) {
				tempObj[array[i]['symbolName']] = {'symbolName' : array[i]['symbolName'], 'companyName' : array[i]['companyName'], 'countryCode' : array[i]['countryCode']};
			};
			for(var item in tempObj) {
				if(tempObj.hasOwnProperty(item)) {
					tempArray.push(tempObj[item])
				};
			};
			return tempArray;
		};

		CNBC_Utils.createCookie('savedViewedSymbols', JSON.stringify(mergedArray), 43800);

	};
  savedSymbolsUpdate();
}

/* file: https://fm-preview.cnbc.com/applications/cnbc.com/resources/files/2018/01/25/quote-synthetic-fix.js?vn=1516894280*/
window.postLoadFunctions = window.postLoadFunctions || {}; window.postLoadFunctions['quotesyntheticfix'] = function() {
  if(symbolInfo.assetSubType== "CNBC Synthetic"){
    $("span:contains('52 week range'), .year-container").hide();
  }
}

/* file: https://fm-preview.cnbc.com/applications/cnbc.com/resources/files/2017/06/09/change-font.js?vn=1497039935*/
if($('span.symbol').text() != "BRK.A"){
	$('.change span.sign-f, .change span.sign-t').css('font-size','28px')
	$('.change').css('padding-left','20px')
}
/* file: https://fm-preview.cnbc.com/applications/cnbc.com/resources/files/2018/05/18/alerts_0.js?vn=1526669158*/
window.postLoadFunctions = window.postLoadFunctions || {}; window.postLoadFunctions['quotebreakingnews'] = function() {
$.ajax({
    url: 'https://www.cnbc.com/services/alerts',
    error: function() {
      $('.cnbc-alert-container-breaking-news').addClass('hide');
      setInterval(bNCall, 30000);

      function bNCall() {
        $.ajax({
            url: 'https://www.cnbc.com/services/alerts',
            error: function() {
              $('#cnbc-alert-container-breaking-news').addClass('hide');
              setInterval(bNCall, 30000);
            },
            success: function(result){
          //  console.log(result);
            if(result.alerts['breaking-news']){
              //  console.log("we have alert: " + result.alerts['breaking-news'][0].url);
                var breakingNewsBanner = '<div id="cnbc-alert-container-breaking-news" class="news-alert breaking-news" style="display: block; margin-bottom:5px;"><div class="content"><div class="news-alert-container"><div class="news-alert-title"><h2 class="headline"><a class="alert-banner" id="breaking-news-link" data-nodeid="'+result.alerts['breaking-news'][0].nid+'" data-regionid="false" href="'+result.alerts['breaking-news'][0].url+'"><span class="alert-title" style="margin-right:5px;">BREAKING:</span><span class="alert-headline">'+result.alerts['breaking-news'][0].headline+'</span></a></h2></div><div class="alert-close-button" id="breaking-news-close-button">×</div><div style="clear:both;"></div></div></div></div>';
                $('#cnbc-alert-container-breaking-news').remove();
                $('#cnbc-new-header').after(breakingNewsBanner);
              }else{
                $('#cnbc-alert-container-breaking-news').addClass('hide');
                setInterval(bNCall, 30000);
              }
            }
          });
        }

    },
    success: function(result){
  //  console.log(result);
    if(result.alerts['breaking-news']){
      //  console.log("we have alert: " + result.alerts['breaking-news'][0].url);

        $('#cnbc-new-header').after('<div id="cnbc-alert-container-breaking-news" class="news-alert breaking-news" style="display: block; margin-bottom:5px;"><div class="content"><div class="news-alert-container"><div class="news-alert-title"><h2 class="headline"><a class="alert-banner" id="breaking-news-link" data-nodeid="'+result.alerts['breaking-news'][0].nid+'" data-regionid="false" href="'+result.alerts['breaking-news'][0].url+'"><span class="alert-title" style="margin-right:5px;">BREAKING:</span><span class="alert-headline">'+result.alerts['breaking-news'][0].headline+'</span></a></h2></div><div class="alert-close-button" id="breaking-news-close-button">×</div><div style="clear:both;"></div></div></div></div>');
        setInterval(bNCall, 30000);

    }else{
      $('#cnbc-alert-container-breaking-news').addClass('hide');
      setInterval(bNCall, 30000);
    }
        function bNCall() {
          $.ajax({
              url: 'https://www.cnbc.com/services/alerts',
              error: function() {
                $('#cnbc-alert-container-breaking-news').addClass('hide');
                setInterval(bNCall, 30000);
              },
              success: function(result){
            //  console.log(result);
              if(result.alerts['breaking-news']){
              //    console.log("we have alert: " + result.alerts['breaking-news'][0].url);
                  var breakingNewsBanner = '<div id="cnbc-alert-container-breaking-news" class="news-alert breaking-news" style="display: block; margin-bottom:5px;"><div class="content"><div class="news-alert-container"><div class="news-alert-title"><h2 class="headline"><a class="alert-banner" id="breaking-news-link" data-nodeid="'+result.alerts['breaking-news'][0].nid+'" data-regionid="false" href="'+result.alerts['breaking-news'][0].url+'"><span class="alert-title" style="margin-right:5px;">BREAKING:</span><span class="alert-headline">'+result.alerts['breaking-news'][0].headline+'</span></a></h2></div><div class="alert-close-button" id="breaking-news-close-button">×</div><div style="clear:both;"></div></div></div></div>';
                  $('#cnbc-alert-container-breaking-news').remove();
                  $('#cnbc-new-header').after(breakingNewsBanner);
                }else{
                $('#cnbc-alert-container-breaking-news').addClass('hide');
                // setInterval(bNCall, 30000);
              }
              }
            });
          }
      }
  });


  $('body').on('click', '#breaking-news-close-button', function() {
    $('#cnbc-alert-container-breaking-news').remove();
});

};

